<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 类型转换陷阱：一个容易被忽视的细节</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .code-block {
            background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            position: relative;
            overflow: hidden;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: rgba(255, 255, 255, 0.05);
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        .code-dots {
            position: absolute;
            top: 14px;
            left: 20px;
            display: flex;
            gap: 8px;
        }
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 60px 30px 30px;
            overflow-x: auto;
        }
        code {
            color: #e6e6e6;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .keyword { color: #c678dd; }
        .type { color: #61afef; }
        .number { color: #d19a66; }
        .string { color: #98c379; }
        .comment { color: #5c6370; font-style: italic; }
        .method { color: #e06c75; }
        .hover-card {
            transition: all 0.3s ease;
        }
        .hover-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        .insight-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            border-left: 4px solid #667eea;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .fade-in {
            animation: fadeIn 0.8s ease-out;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-purple-900 via-purple-700 to-indigo-800 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center fade-in">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    Java 类型转换陷阱
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    一个容易被忽视的细节，可能让你的代码出现意想不到的结果
                </p>
                <div class="flex justify-center items-center space-x-4 text-sm">
                    <span class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        Java 基础
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-clock mr-2"></i>
                        5 分钟阅读
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-lightbulb mr-2"></i>
                        实战案例
                    </span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 110C120 100 240 80 360 70C480 60 600 60 720 65C840 70 960 80 1080 85C1200 90 1320 90 1380 90L1440 90V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0V120Z" fill="#f9fafb"/>
            </svg>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-5xl">
        <!-- Problem Introduction -->
        <section class="mb-16 fade-in">
            <div class="bg-white rounded-2xl shadow-lg p-8 hover-card">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-exclamation-triangle text-yellow-500 mr-3"></i>
                    问题场景
                </h2>
                <p class="text-lg text-gray-700 leading-relaxed mb-8">
                    让我们先看一段看似简单的代码，它试图向 HashSet 中添加元素，然后立即删除前一个元素。你能预测出最终集合的大小吗？
                </p>
                
                <!-- Original Code -->
                <div class="code-block mb-8">
                    <div class="code-dots">
                        <div class="dot dot-red"></div>
                        <div class="dot dot-yellow"></div>
                        <div class="dot dot-green"></div>
                    </div>
                    <pre><code><span class="type">Set</span>&lt;<span class="type">Short</span>&gt; set = <span class="keyword">new</span> <span class="type">HashSet</span>&lt;&gt;();
<span class="keyword">for</span> (<span class="type">short</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) {
    set.<span class="method">add</span>(i);
    set.<span class="method">remove</span>(i - <span class="number">1</span>);
}
<span class="type">System</span>.out.<span class="method">println</span>(set.<span class="method">size</span>());</code></pre>
                </div>

                <div class="bg-red-50 border-l-4 border-red-500 p-6 rounded-r-lg">
                    <p class="text-lg font-semibold text-red-800 mb-2">
                        <i class="fas fa-times-circle mr-2"></i>
                        运行结果：5
                    </p>
                    <p class="text-gray-700">
                        意外吗？集合中的元素一个都没有被删除！
                    </p>
                </div>
            </div>
        </section>

        <!-- Type Conversion Visualization -->
        <section class="mb-16 fade-in">
            <div class="bg-white rounded-2xl shadow-lg p-8 hover-card">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-microscope text-blue-500 mr-3"></i>
                    深入分析
                </h2>
                
                <div class="mermaid">
                    <div class="mermaid">
                        graph TD
                            A[i: short 类型] -->|减法运算| B[i - 1]
                            C[1: int 类型] -->|参与运算| B
                            B -->|类型提升| D[结果: int 类型]
                            D -->|传入 remove 方法| E[remove 参数: Object]
                            F[HashSet 中存储: Short 类型] -->|类型不匹配| G[删除失败]
                            E --> G
                            
                            style A fill:#e0f2fe,stroke:#0284c7,stroke-width:2px
                            style C fill:#fef3c7,stroke:#f59e0b,stroke-width:2px
                            style D fill:#fee2e2,stroke:#dc2626,stroke-width:2px
                            style G fill:#fecaca,stroke:#ef4444,stroke-width:3px
                    </div>
                </div>

                <div class="grid md:grid-cols-2 gap-6 mt-8">
                    <div class="insight-card p-6 rounded-lg">
                        <h3 class="text-xl font-bold mb-3 text-purple-800">
                            <i class="fas fa-info-circle mr-2"></i>
                            关键点 1：类型提升
                        </h3>
                        <p class="text-gray-700">
                            当 short 类型与 int 类型进行运算时，结果会自动提升为 int 类型。这是 Java 的类型提升规则。
                        </p>
                    </div>
                    <div class="insight-card p-6 rounded-lg">
                        <h3 class="text-xl font-bold mb-3 text-purple-800">
                            <i class="fas fa-info-circle mr-2"></i>
                            关键点 2：对象比较
                        </h3>
                        <p class="text-gray-700">
                            HashSet 的 remove 方法使用 equals 比较对象。Short 和 Integer 是不同的类型，equals 返回 false。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Solution -->
        <section class="mb-16 fade-in">
            <div class="bg-white rounded-2xl shadow-lg p-8 hover-card">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-check-circle text-green-500 mr-3"></i>
                    正确的解决方案
                </h2>
                
                <p class="text-lg text-gray-700 mb-6">
                    要解决这个问题，我们需要确保传递给 remove 方法的参数类型与集合中存储的类型一致：
                </p>

                <!-- Corrected Code -->
                <div class="code-block mb-8">
                    <div class="code-dots">
                        <div class="dot dot-red"></div>
                        <div class="dot dot-yellow"></div>
                        <div class="dot dot-green"></div>
                    </div>
                    <pre><code><span class="type">Set</span>&lt;<span class="type">Short</span>&gt; set = <span class="keyword">new</span> <span class="type">HashSet</span>&lt;&gt;(); <span class="comment">// 0 1 2 3 4</span>
<span class="keyword">for</span> (<span class="type">short</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) {
    set.<span class="method">add</span>(i);
    <span class="type">short</span> x = (<span class="type">short</span>) (i - <span class="number">1</span>); <span class="comment">// 把计算后的结果转 short</span>
    set.<span class="method">remove</span>(x);
}
<span class="type">System</span>.out.<span class="method">println</span>(set.<span class="method">size</span>());</code></pre>
                </div>

                <div class="bg-green-50 border-l-4 border-green-500 p-6 rounded-r-lg">
                    <p class="text-lg font-semibold text-green-800 mb-2">
                        <i class="fas fa-check-circle mr-2"></i>
                        运行结果：1
                    </p>
                    <p class="text-gray-700">
                        现在代码按预期工作了！最终只剩下元素 4。
                    </p>
                </div>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-16 fade-in">
            <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-8 text-center gradient-text">
                    最佳实践建议
                </h2>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white p-6 rounded-xl shadow hover-card">
                        <div class="text-4xl text-purple-600 mb-4 text-center">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-center">类型一致性</h3>
                        <p class="text-gray-700 text-center">
                            在使用集合时，始终确保操作的数据类型与集合声明的